package threadpool;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 描述：演示关闭线程池  same as corePoolSize
 */
public class ShutDown {

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 100; i++) {
            executorService.execute(new ShutDownTask());
        }
        Thread.sleep(1500);
        //用于立即关闭线程池，即取消所有正在执行的任务并中断所有工作线程。返回一个包含未执行的任务列表
//        List<Runnable> runnableList = executorService.shutdownNow();

//        executorService.shutdown(); //用于关闭线程池，即不再接受新的任务，但会等待已经提交的任务执行完毕
//        executorService.execute(new ShutDownTask());
        //用于检测线程池是否关闭。调用此方法会阻塞当前线程，直到线程池关闭，或者超时，或者当前线程被中断
//        boolean b = executorService.awaitTermination(7L, TimeUnit.SECONDS);
//        System.out.println(b);
        System.out.println("线程池是否关闭=====>" + executorService.isShutdown());//用于判断线程池是否关闭
        executorService.shutdown();
        System.out.println(executorService.isShutdown());
        System.out.println("线程池是否已经完全终止=====>" + executorService.isTerminated()); //用于判断线程池是否已经完全终止（任务执行完毕，所有线程都关闭）
        Thread.sleep(10000);
        System.out.println(executorService.isTerminated());

//        executorService.execute(new ShutDownTask());
    }
}

class ShutDownTask implements Runnable {


    @Override
    public void run() {
        try {
            Thread.sleep(500);
            System.out.println(Thread.currentThread().getName());
        } catch (InterruptedException e) {
            System.out.println(Thread.currentThread().getName() + "被中断了");
        }
    }
}
